package lwf.handler.math;

import java.util.List;

import net.lwf.entity.AttemptedQuestion;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.NumberFormatter;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import lwf.handler.helper.FormatAttribute;
import lwf.handler.util.DataProcessor;

public class ArithmaticsProcessHandler extends ProcessHandler implements IProcessHandler {
	
	private String questionDesc = "";
	private FormatAttribute format;
	
	public ArithmaticsProcessHandler(){
		
	}
	public ArithmaticsProcessHandler(String datastring) {
		DataProcessor.getSumDetail(datastring, this);
		setFormatUserAnswer(true);
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable(generateQuestionTable());
		format.setQuestionType(2); 
		
		return new DisplayFormatter(format);
	}

	/**
	 * @return Returns the questiondesc.
	 */
	public String getQuestionDesc() {
		StringBuffer sbQ = new StringBuffer();
		if(this.getOperation().equals(IHandlerConstant.ADDITION)) {
			List data = this.getAdddata();
			for(int i=0; i<data.size(); i++) {
				String qData = (String) data.get(i);
				String displayFormat = NumberFormatter.getFormat(getDecimalDigits());
				sbQ.append(NumberFormatter.format(qData, displayFormat));
				if(i+1 < data.size()) {
					sbQ.append(" ");
					sbQ.append(this.getOperationsign());
					sbQ.append(" ");
				}
				else {
					sbQ.append(" = ");
				}
			}
		}
		else {
			String displayFormat = NumberFormatter.getFormat(getDecimalDigits());
			sbQ.append(NumberFormatter.format(this.getData1(), displayFormat));
			sbQ.append(" ");
			sbQ.append(this.getOperationsign());
			sbQ.append(" ");	
			sbQ.append(NumberFormatter.format(this.getData2(), displayFormat));
			sbQ.append(" ");
			sbQ.append(" = ");
		}
		return sbQ.toString();
	}
	
	//---------------------------------------------------------------------------------------------------
	// Private methods
    //---------------------------------------------------------------------------------------------------
	
	private String generateQuestionTable() {
		String displayFormat = NumberFormatter.getFormat(getDecimalDigits());
		StringBuffer sbQ = new StringBuffer();
		if(this.getOperation().equals(IHandlerConstant.ADDITION)) {
			List data = this.getAdddata();
			for(int i=0; i<data.size(); i++) {
				String qData = (String) data.get(i);
				sbQ.append(NumberFormatter.format(qData, displayFormat));
				if(i+1 < data.size()) {
					sbQ.append(" ");
					sbQ.append(this.getOperationsign());
					sbQ.append(" ");
				}
				else {
					sbQ.append(" = ");
				}
			}
		}
		else {
			sbQ.append(NumberFormatter.format(this.getData1(), displayFormat));
			sbQ.append(" ");
			sbQ.append(this.getOperationsign());
			sbQ.append(" ");	
			sbQ.append(NumberFormatter.format(this.getData2(), displayFormat));
			sbQ.append(" ");
			sbQ.append(" = ");
		}
		
		StringBuffer sbQBlock = new StringBuffer("<table width=\"100%\" border=\"0\" >");
		sbQBlock.append("<tr>");
		sbQBlock.append("<td class=\"data\" >");
		sbQBlock.append(sbQ.toString());
		sbQBlock.append("</td>");
		sbQBlock.append("<td class=\"question\" >");
		sbQBlock.append("?");
		sbQBlock.append("</td>");
		sbQBlock.append("</tr>");
		sbQBlock.append("</table>");
		
		return sbQBlock.toString();
	}

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}
	@Override
	public String getPdfInstructionalText() {
		return null;
	}
	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}
	@Override
	public String getPdfNote() {
		return null;
	}

} //ArithmaticsProcessHandler
